<template>
	<view>
		<view :id="chartId" 
			:chartId="chartId" :change:chartId="echarts.bindData"
			:prop="option" :change:prop="echarts.updateEcharts"
			:style="{width: '100%',height: height + 'px'}" @click="echarts.onClick"></view>
	</view>
</template>

<script>
	export default {
		name: "renderCharts",
		props: {
			chartId: {
				type: String,
				default: 'chart',
				required: true
			},
			option: {
				type: Object,
				default: null
			},
			height: {
				type: Number,
				default: 250
			},
		},
		data() {
			const v = this
			return {
			}
		},
		methods: {
			onViewClick(options) {
				// console.log(options)
			}
		}
	}
</script>
<script module="echarts" lang="renderjs">
	let myChart
	export default {
		mounted() {
			if (typeof window.echarts === 'function') {
				this.initEcharts()
			} else {
				// 动态引入较大类库避免影响页面展示
				const script = document.createElement('script')
				// view 层的页面运行在 www 根目录，其相对路径相对于 www 计算
				script.src = 'static/js/echarts/echarts.js'
				script.onload = this.initEcharts.bind(this)
				document.head.appendChild(script)
			}
		},
		methods: {
			initEcharts() {
				myChart = echarts.init(document.getElementById(this.chartId))
				// 观测更新的数据在 view 层可以直接访问到
				if (this.option) myChart.setOption(this.option)
			},
			updateEcharts(newValue, oldValue, ownerInstance, instance) {
				// 监听 service 层数据变更
				myChart.setOption(newValue)
			},
			onClick(event, ownerInstance) {
				// 调用 service 层的方法
				ownerInstance.callMethod('onViewClick', {
					test: 'test'
				})
			},
			bindData () {
				
			}
		}
	}
</script>

